/**
 *  Timestamp Manager
 */
package kts;

import java.util.Hashtable;

/**
 * @author jaime
 */
public class TsManager {

	private Hashtable<String,Timestamp> timestamps;
	
	public TsManager()
	{
		timestamps = new Hashtable<String,Timestamp>();	
	}
	
	public void updateTs(String key, int ts)
	{
		timestamps.put(key, new Timestamp(key, ts, true));
	}
	
	private Boolean indirectInit(String key, int localTs)
	{
		timestamps.put(key, new Timestamp(key, localTs, true));
		return true;
	}
	
	public int getTs(String key, int localTs)
	{
		Timestamp ts = timestamps.get(key);
		
		if (ts == null || ts.status == false)
		{
			this.indirectInit(key, localTs);	
		} 
		ts = timestamps.get(key);
		if (ts != null)
		{
			ts.counter++;
			return ts.counter;
		} else return -1;
	}
	
	public int lastTs(String key)
	{
		Timestamp ts = timestamps.get(key);
		if (ts != null)
			return ts.counter;
		else 
			return -1;
	}
}